import FrameComponent from "./FrameComponent";

class FrameUtils {
  private step = 0;
  private replayData: ReplayData;
  private currentFrameData: FrameData;
  frameComponent: FrameComponent;

  recordRotation() {

  }

  recordAcc() {

  }

  recordTouchStart(id: number, startX: number, startY: number) {
    this.currentFrameData.controlsData.touchStart = {id, startX, startY}
  }

  recordTouchMove(locationX: number, locationY: number) {
    this.currentFrameData.controlsData.touchMove = {locationX, locationY}
  }

  recordTouchEnd() {
    this.currentFrameData.controlsData.touchEnd = {}
  }

  getFrameData(): FrameData {
    return this.replayData.data.shift();
  }

  update() {
    this.step += 1;
    const frameData: FrameData = {step: this.step, controlsData: {}};
    this.currentFrameData = frameData;
    this.replayData.data.push(frameData)
  }

  reset() {
    this.step = 0;
    this.replayData = {data: []};
  }

  replay() {
    this.frameComponent.startReplay();
  }
}

interface ReplayData {
  data: Array<FrameData>
}

interface FrameData {
  step: number;
  controlsData: ControlData;
}

interface ControlData {
  touchStart?: { id: number, startX: number, startY: number },
  touchMove?: { locationX: number, locationY: number },
  touchEnd?: {}
}

export function frameUpdate() {
  return function (target: cc.Component, propertyKey: string, descriptor: PropertyDescriptor) {
    let func = descriptor.value;
    descriptor.value = function (...args) {
      if (this.enabled === true && this.node.active === true) {
        func.apply(this, args)
      }
    }
  }
}

// export const frameUtils = new FrameUtils();